//
//  ListView.m
//  Project two framework
//
//  Created by rimi on 16/7/26.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ListView.h"
#import "ZPZNetRequest+MealsList.h"
#import "ZZConfig.h"
#import "SingleView.h"
#import "ShoprDataSource.h"
@interface ListView ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UITextField *search;
@property (nonatomic, strong) UIButton *searchImage;
@property (nonatomic, strong) NSString *keyWord;
@property (nonatomic, strong) NSDictionary *numberDic;

@end

@implementation ListView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self interface];
        self.backgroundColor = COLOR(239, 239, 239, 1);
    }
    return self;
}

- (void)interface
{
    _keyWord = @"";
    [self addSubview:self.search];
    [self addSubview:self.searchImage];
    [self addSubview:self.tableView];
    [self loadDataSourceWithKeyWord:self.keyWord];
    //注册通知监听输入文字的变化
    [self.search addTarget:self action:@selector(textChangeAction) forControlEvents:UIControlEventEditingChanged];
    /** 从购买框里面传过来的通知 */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sureShopping:) name:@"shopingAction" object:nil];
}


-(void)loadDataSourceWithKeyWord:(NSString *)keyWord{
    _dataSource = [NSMutableArray array];
   [ZPZNetRequest getAllProductsListKeywords:keyWord UserId:@"22" comletion:^(id object, BOOL success) {
       if (success) {
           NSLog(@"==============%@",object);
           [_dataSource addObjectsFromArray:object[@"content"]];
           [_tableView reloadData];
       }
   }];
  
    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self loadDataSourceWithKeyWord:textField.text];
    [textField resignFirstResponder];
    return YES;
}


- (void)searchAction:(UIButton *)sender
{
    [self loadDataSourceWithKeyWord:_search.text];
}

//监听文字变化
-(void)textChangeAction{
    
    [self loadDataSourceWithKeyWord:_search.text];
    
}
#pragma mark - 通知的实现
- (void)sureShopping:(NSNotification *)info
{
    _numberDic = info.userInfo;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.backgroundColor = COLOR(239, 239, 239, 1);
    cell.textLabel.text = _dataSource[indexPath.row][@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [SingleView shareValue].singDic = [NSMutableDictionary dictionaryWithDictionary: _dataSource[indexPath.row]];
    [SingleView showOtherView];
}

#pragma mark - 懒加载
- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = ({
            UITableView * tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.bounds.size.width, self.bounds.size.height - 50)];
            tableView.rowHeight = 45;
            tableView.delegate = self;
            tableView.dataSource = self;
            
            tableView;
        
        });
    }
    return _tableView;
}

- (UITextField *)search
{
    if (_search == nil) {
        _search = ({
            UITextField *search = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 230, 50)];
            search.placeholder = @"Search";
            search.delegate = self;
            search.font = [UIFont systemFontOfSize:25];
            [search setBackground:[UIImage imageNamed:@"搜索框_11.png"]];
            search;
        });
    }
    return _search;
}

- (UIButton *)searchImage
{
    if (_searchImage == nil) {
        _searchImage = ({
            UIButton *searchImage = [UIButton buttonWithType:UIButtonTypeCustom];
            searchImage.frame = CGRectMake(230, 0, 50, 50);
            [searchImage setImage:[UIImage imageNamed:@"search1_23.png"] forState:UIControlStateNormal];
            [searchImage addTarget:self action:@selector(searchAction:) forControlEvents:UIControlEventTouchUpInside];
            searchImage;
        });
    }
    return _searchImage;
}




@end
